import store from "../store";

//声明 reducer 函数
let countReducer = (state = 10, action) => {
  //函数内部, 要根据 action 操作对象, 对 state 进行管理
  switch (action.type) {
    case "INCRE":
      //增加 state 的值  『返回修改后的状态』
      return state + action.payload;
    case "DECRE":
      //减少 state 的值
      return state - action.payload;
    default:
      //其他   『这个步骤一定要做』, 一定初始化的时候, 需要使用 state 的值进行内部状态的赋值
      return state;
  }
}

//封装函数, 创建 action 对象    store.dispatch(increAction(100));
export function increAction(payload) {
  return {
    type: "INCRE",
    payload: payload,
  };
}

//简写
export let decreAction = (payload) => ({ type: "DECRE", payload });

//封装函数, 返回要 dispatch 的函数
export function asyncIncreAction(payload) {
  return (dispatch) => {
    setTimeout(() => {
      dispatch(increAction(payload));
    }, 1000);
  };
}


export default countReducer;
